import json
import requests
from utils.sendmethod import Send_Method
from interface.mall_ums_member_controller import UmsMemberController


class ShoppingProcess(Send_Method):
    mall_ip = "http://47.108.206.100:8085"

    def add_shopping_cart(self):
        """添加到购物车"""
        url = self.mall_ip.join('/cart/add')
        token = {'bearer_token': UmsMemberController.sso_login()}
        body = {
            "createDate": "2023-05-24T11:18:13.395Z",
            "deleteStatus": 0,
            "memberId": 27,
            "modifyDate": "2023-05-24T11:18:13.395Z",
            "price": 2699.00,
            "productAttr": "[{\"key\":\"颜色\",\"value\":\"黑色\"},{\"key\":\"容量\",\"value\":\"32G\"}]",
            "productBrand": "小米",
            "productCategoryId": 19,
            "productId": 27,
            "productName": "小米8 全面屏游戏智能手机 6GB+64GB 黑色 全网通4G 双卡双待",
            "productPic": "shenzhen.aliyuncs.com/mall/images/20180615/xiaomi.jpg",
            "productSkuCode": "201808270027001",
            "productSkuId": 98,
            "productSn": "7437788",
            "productSubTitle": "骁龙845处理器，红外人脸解锁，AI变焦双摄，AI语音助手小米6X低至1299，点击抢购",
            "quantity": 1}
        respon = self.send_Method(url, headers=token, data=json.dumps(body))
        return respon.json()

    def get_obtain_shopping_info(self):
        """获取当前会员购物信息"""
        url = self.mall_ip.join('/cart/list')
        token = {'bearer_token': UmsMemberController.sso_login()}
        respon = requests.Request.json(self.send_Method(url, headers=token))
        return respon[0][id]

    def generate_order(self):
        """根据购物车生成订单"""
        url = self.mall_ip.join('/order/generateOrder')
        token = {'bearer_token': UmsMemberController.sso_login()}
        body = {
            "cartIds": [
                f"{self.add_shopping_cart()}"
            ],
            "memberReceiveAddressId": 23,
            "payType": 1

        }
        respon = self.send_Method(url, headers=token, data=json.dumps(body))
        return respon.json()

    def confirm_order(self):
        """根据订单购物车信息确认订单"""
        url = self.mall_ip.join('/order/generateConfirmOrder')
        token = {'bearer_token': UmsMemberController.sso_login()}
        respon = self.send_Method(url, headers=token)
        return respon.json()
